package service;

import dao.RentMapper;
import entity.Rent;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.session.SqlSession;
import utils.MyBatisUtil;

import java.util.ArrayList;
import java.util.List;

public class RentService {
    /**
     * 查询方法，查询所有车位租赁信息，查询结果租赁状态升序排序（租赁状态数据靠前排列）、到期日期降序排列
     * @param pageIndex
     * @param pageSize
     * @return
     */
    public List<Rent> list(@Param("pageIndex")int pageIndex, @Param("pageSize")int pageSize){
        List<Rent> list = new ArrayList<Rent>();
        SqlSession sqlSession =null;
        try{
        sqlSession= MyBatisUtil.createSqlSession();
        list=sqlSession.getMapper(RentMapper.class).list(pageIndex, pageSize);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }

        return list;
    }

    /**
     * 添加方法，将新的车位租赁信息保存到数据库
     * @param rent
     * @return
     */
    public int add(Rent rent){
        int count =0;
        SqlSession sqlSession =null;
        try{
            sqlSession= MyBatisUtil.createSqlSession();
            count=sqlSession.getMapper(RentMapper.class).add(rent);
            sqlSession.commit();
        }catch (Exception e){
            e.printStackTrace();
            sqlSession.rollback();
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        return count;
    }

    /**
     * 根据id删除租赁信息
     * @param id
     * @return
     */
    public int del(@Param("id")Integer id){
        int count =0;
        SqlSession sqlSession =null;
        try{
            sqlSession= MyBatisUtil.createSqlSession();
            count=sqlSession.getMapper(RentMapper.class).del(id);
            sqlSession.commit();
        }catch (Exception e){
            e.printStackTrace();
            sqlSession.rollback();
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        return count;
    }
    /**
     * 修改方法，修改知道的车位租赁信息，将租赁状态改为"已到期"
     * @param id
     * @param status
     * @return
     */
    public int update(@Param("status")int status,@Param("id") Integer id){
        int count = 0;
        SqlSession sqlSession =null;
        try{
            sqlSession= MyBatisUtil.createSqlSession();
            count=sqlSession.getMapper(RentMapper.class).update(status, id);
            sqlSession.commit();
        }catch (Exception e){
            e.printStackTrace();
            sqlSession.rollback();
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        return count;
    }

    /**
     * 搜索框查询列表
     * @param userName
     * @param status
     * @return
     */
    public List<Rent> souS(@Param("userName")String userName ,@Param("status")int status){
        List<Rent> list = new ArrayList<Rent>();
        SqlSession sqlSession = null;
        try{
            sqlSession =MyBatisUtil.createSqlSession();
            list = sqlSession.getMapper(RentMapper.class).souS(userName, status);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        return list;
    }
}
